package zhangqi.com.getcontacts;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import zhangqi.com.getcontacts.domin.Contact;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void click(View view) {
        //通过内容提供者访问联系人数据库
        ContentResolver cr = getContentResolver();
        Cursor cursorContactId = cr.query(Uri.parse("content://com.android.contacts/raw_contacts"),
                new String[]{"contact_id"}, null, null, null);
        while (cursorContactId.moveToNext()){
            String contactId = cursorContactId.getString(0);
            Cursor cursorData = cr.query(Uri.parse("content://com.android.contacts/data"),
                    new String[]{"data1", "mimetype"}, "raw_contact_id=?", new String[]{contactId}, null);
            Contact con = new Contact();
            while (cursorData.moveToNext()){
                String data1= cursorData.getString(0);
                String mimetype = cursorData.getString(1);
                //通过mimtetype的判断，把data1存入对应的属性
                if("vnd.android.cursor.item/email_v2".equals(mimetype)){
                    con.setEmail(data1);
                }else if("vnd.android.cursor.item/phone_v2".equals(mimetype)){
                    con.setPhone(data1);
                }else if("vnd.android.cursor.item/name".equals(mimetype)){
                    con.setName(data1);
                }
            }
            Log.i("zhangqi","得到的联系人是"+con.toString());
        }
    }

    public void click2(View view) {
        ContentResolver cr = getContentResolver();
        //先查询raw_contacts表，获取最新联系人的主键，然后主键+1，就是要插入的联系人的id
        Cursor cursorContactId = cr.query(Uri.parse("content://com.android.contacts/raw_contacts"),
                new String[]{"_id"},null,null,null);
        //默认联系人id就是1
        int contact_id = 1;
        if(cursorContactId.moveToLast()){
            //拿到主键
            int _id = cursorContactId.getInt(0);
            //主键+1，就是要插入的联系人id
            contact_id = ++_id;
        }
        ContentValues values = new ContentValues();
        values.put("contact_id",contact_id);
        //把联系人id插入raw_contacts数据库
        cr.insert(Uri.parse("content://com.android.contacts/raw_contacts"),values);

        values.clear();
        values.put("data1","2b");
        values.put("mimetype","vnd.android.cursor.item/name");
        values.put("raw_contact_id",contact_id);
        cr.insert(Uri.parse("content://com.android.contacts/data"),values);

        values.clear();
        values.put("data1","1546564123");
        values.put("mimetype","vnd.android.cursor.item/phone_v2");
        values.put("raw_contact_id",contact_id);
        cr.insert(Uri.parse("content://com.android.contacts/data"),values);
    }
}















